from datetime import datetime

import grpc

from grpc_interceptor_test.proto import hello_pb2, hello_pb2_grpc


class DefaultInterceptor(grpc.UnaryUnaryClientInterceptor):
    def intercept_unary_unary(self, continuation, client_call_details, request):
        start = datetime.now()
        rsp = continuation(client_call_details, request)
        print((datetime.now() - start).microseconds / 1000)
        return rsp


if __name__ == "__main__":
    default_interceptor = DefaultInterceptor()
    with grpc.insecure_channel("localhost:50051") as channel:
        intercept_channel = grpc.intercept_channel(channel, default_interceptor)
        stub = hello_pb2_grpc.GreeterStub(intercept_channel)
        hello_request = hello_pb2.HelloRequest()
        hello_request.name = "bobby"
        rsp: hello_pb2.HelloReply = stub.SayHello(hello_request)

        print(rsp.message)
